var MAXPLAYERCOUNT = 45,
	WINNERCOUNT = 3,	//每局胜利者的数量
	DOWNTIME = 3,	//每局游戏的时间
	currentPlayerCount = 0,
	currentWinnerCount = 0,
	queryPayerTimer = null,
	queryWinnerTimer = null;

var countConfig = {
    timeText:  '', //倒计时时间，格式：年/月/日 时:分:秒
    timeZone:'8' , //时区，GMT号码
    style: "flip", //显示的样式，可选值有flip,slide,metal,crystal(翻动、滑动、金属、水晶)
    color: "white", //显示的颜色，可选值white,black(黑色、白色)
    width: 0, //倒计时宽度，无限制，默认为0
    textGroupSpace: 15, //天、时、分、秒之间间距
    textSpace: 0, //数字之间间距
    reflection: !1, //是否显示倒影
    reflectionOpacity: 10, //倒影透明度
    reflectionBlur: 0, //倒影模糊程度
    dayTextNumber: 3, //倒计时天数数字个数
    displayDay: !1, //是否显示天数
    displayHour: !1, //是否显示小时数
    displayMinute: !0, //是否显示分钟数
    displaySecond: !0, //是否显示秒数
    displayLabel: !0, //是否显示倒计时底部label
    onFinish: function() {stopGame();}//完成事件，您可以在时间结束时执行一些脚本，在创建倒计时时只需传递一个函数即可。
};

displayPlayerSection();

function displayPlayerSection(){
	$("section.game-winner").hide();
	$("section.game-player").show();
}

function displayLuckySection(){
	$("section.lucky-player").show();
}

function displayWinnerSection(){
	$("section.game-player").hide();
	$("section.game-winner").show();
	$('.carousel').carousel({
	  	interval: 3000,
	  	pause: null
	});
	currentWinnerCount = 0;
	var countdownTime = moment().add(DOWNTIME,"minutes");
	countConfig.timeText = countdownTime.format("YYYY/MM/DD HH:mm:ss");
	console.log(countConfig.timeText);
	$(".countdown").jCountdown(countConfig);
	queryWinnerTimer = window.setInterval(queryWinner,5000);
}

//开始游戏
function startGame(){
	console.log("startGame");
	currentPlayerCount = 0;
	queryPayerTimer = window.setInterval(queryPayer,200);
	$("button.reset").removeAttr("disabled");
	$("button.start").attr("disabled","disabled");
}

//重新开始游戏
function replayGame(){
	if(queryPayerTimer){
		window.clearInterval(queryPayerTimer);
	}
	$("button.start").removeAttr("disabled");
	$("button.reset").attr("disabled","disabled");
	displayPlayerCount(0);
	currentPlayerCount = 0;
	$(".player-list").html("");

}

//结束游戏
function stopGame(){
	window.clearInterval(queryWinnerTimer);
	$(".countdown").jCountdown("stop");
	$("button.lucky").show();
}

//轮询查询参与者
function queryPayer(){
	var playerList = Mock.mock({
		"payerlist|1": [
			{
				"headlogo": Mock.Random.dataImage("50x50"),
				"nickname": "@cword(2,6)"
			}
		]
	}).payerlist;
	appendPayer("player", playerList);
}

//轮询查询获胜者
function queryWinner(){
	var playerList = Mock.mock({
		"payerlist|9": [
			{
				"headlogo": Mock.Random.dataImage("50x50"),
				"nickname": "@cword(2,6)"
			}
		]
	}).payerlist;
	var winner = document.createElement("ul"),
		id = "winner"+currentWinnerCount;
	winner.id = id;
	winner.className="player-list";
	$("#winner").append(winner);
	appendWinner(id, playerList);
	
}

var currentTimes = 0,
	intervalId = null,
	headerLogoIndex = -1;
/**
 * 抽取幸运者
 * y=1000cos(π/54x)+1020
 */
function randomLuckyPlayer(){
	$(".game-winner").hide();
	$(".lucky-player").show();
	var playerList = Mock.mock({
		"list|2-5": [
			{
				"payerlist|9": [
					{
						"headlogo": Mock.Random.dataImage("50x50"),
						"nickname": "@cword(2,6)"
					}
				]
			}
		]
	}).list;
	var count = playerList.length;
	var T = 108*8,
		Max = Mock.Random.integer(T*2-8,T*2-1),
		A = 100,
		K = 120,
		totalTimes = 0;
	$("#lucky-totalcount").text(count);
	for(var i=0; i<count; i++){

		(function(count){
			window.setTimeout(function(){
				$("#lucky-count").text(count);
			},totalTimes);
		})(i+1);

		playerList[i].payerlist.forEach(function(player){
			var li = '<li><img src="'+ player.headlogo+'" /></li>';	
			$("ul.lucky-list").append(li);
		});
		headerLogoIndex = -1;
		for(var x=parseInt(T/4); x<=T; x=x+4){
			var y = parseInt(A*Math.cos(Math.PI/T*x)+K);
			totalTimes = totalTimes + y;
			animateLuckyPlayer(totalTimes);
		}

		(function(payerlist){
			window.setTimeout(function(){
				appendLucky("luckylist", payerlist[headerLogoIndex]);
			},totalTimes);
		})(playerList[i].payerlist);

		//每轮结束后延时2s
		totalTimes = totalTimes + 2000;
		
	}
}

/**
 * @param {interval} 时间间隔
 * @param {times} 次数
 */
/*function animateLuckyPlayer(interval,times){
	currentTimes = 0;
	intervalId = window.setInterval(function(){
		if(currentTimes > times){
			window.clearInterval(intervalId);
		}else{
			headerLogoIndex = (headerLogoIndex+1) % 9;
			console.log(headerLogoIndex);
			$("ul.lucky-list li.active").removeClass("active");
			$("ul.lucky-list li").eq(headerLogoIndex).addClass("active");
		}
		currentTimes++;
	},interval);
}*/

function animateLuckyPlayer(interval){
	intervalId = window.setTimeout(function(){
		headerLogoIndex = (headerLogoIndex+1) % 9;
		$("ul.lucky-list li.active").removeClass("active");
		$("ul.lucky-list li").eq(headerLogoIndex).addClass("active");
	},interval);
}

//显示参与的人数
function displayPlayerCount(count){
	$("#playerCount").text(count);
}

/**
 * 追加获胜者
 */
function appendWinner(id, playerList){
	currentWinnerCount++;
	playerList.forEach(function(player){
		var li = '<li><img src="'+ player.headlogo+'" /><span>'+player.nickname+'</span></li>';
		$("#"+id).append(li);
	});
	if(currentWinnerCount>=WINNERCOUNT){
		stopGame();
	}
}

/**
 * 追加参与者
 */
function appendPayer(id, player){
	if(currentPlayerCount > MAXPLAYERCOUNT) return;
	currentPlayerCount++;
	displayPlayerCount(currentPlayerCount);
	var li = '<li><img src="'+ player.headlogo+'" /><span>'+player.nickname+'</span></li>';
	$("#"+id).append(li);
	//参与人数达到最大人数
	if(currentPlayerCount >= MAXPLAYERCOUNT){
		window.clearInterval(queryPayerTimer);
		// displayWinnerSection();
		$("button.start").hide();
		$("button.startgame").show();
	}
}

/**
 * 追加幸运者
 */
function appendLucky(id, player){
	var li = '<li><img src="'+ player.headlogo+'" /><span>'+player.nickname+'</span></li>';
	$("#"+id).append(li);
}

$("button.start").on("click",startGame);
$("button.startgame").on("click",displayWinnerSection);
$("button.reset").on("click",replayGame);
$("button.lucky").on("click",randomLuckyPlayer)